package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.InquiryMapper;
import com.link_with_health.mapper.PrescriptionMapper;
import com.link_with_health.mapper.TalkRoomMapper;
import com.link_with_health.model.InquiryEntity;
import com.link_with_health.service.BaseService;
import com.link_with_health.service.InquiryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("inquiryService")
public class InquiryServiceImpl extends BaseService implements InquiryService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    InquiryMapper inquiryMapper;
    @Resource
    PrescriptionMapper prescriptionMapper;
    @Resource
    TalkRoomMapper talkRoomMapper;

    /**
     * 通过问诊id获取问诊信息
     * @param caseId 问诊信息
     * @return 问诊信息
     * @throws BaseException 异常
     */
    @Override
    public InquiryEntity getInquiryById(Long caseId) throws BaseException {
        InquiryEntity entity = null;
        try {
            entity = inquiryMapper.queryInquiryById(caseId);
        } catch (Exception e) {
            logger.error("[query inquiry failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("INQUIRY.CASE_ID");
        return entity;
    }

    /**
     * 通过患者信息获取问诊信息
     * @param patientId 患者信息
     * @return 问诊信息
     * @throws BaseException 异常
     */
    @Override
    public List<InquiryEntity> getInquiryByPatientId(Long patientId) throws BaseException {
        List<InquiryEntity> inquiryList = null;
        try {
            inquiryList = inquiryMapper.queryInquiryByPatientId(patientId);
        } catch (Exception e) {
            logger.error("[query inquiry failed]",e);
        }
        if (inquiryList == null || inquiryList.isEmpty())
            throw new NullQueryResultException("INQUIRY.PATIENT_ID: "+patientId);
        return inquiryList;
    }

    /**
     * 通过医生id获取会诊信息
     * @param DrId 医生id
     * @return  会诊信息
     * @throws BaseException 异常
     */
    @Override
    public List<InquiryEntity> getInquiryByDrId(Long DrId) throws BaseException {
        List<InquiryEntity> inquiryList = null;
        try {
            inquiryList = inquiryMapper.queryInquiryByDrId(DrId);
        } catch(Exception e) {
            logger.error("[query inquiry failed]",e);
        }

        if (inquiryList == null || inquiryList.isEmpty())
            throw new NullQueryResultException("INQUIRY.DR_ID: "+DrId);

        return inquiryList;
    }

    /**
     * 通过用户id获取会诊信息
     * @param userId 用户id
     * @return  会诊信息
     * @throws BaseException 异常
     */
    @Override
    public List<InquiryEntity> getInquiryByUserId(Long userId) throws BaseException {
        List<InquiryEntity> inquiryList = null;
        try {
            inquiryList = inquiryMapper.queryInquiryByUserId(userId);
        } catch (Exception e) {
            logger.error("[query inquiry failed]",e);
        }
        if (inquiryList == null || inquiryList.isEmpty())
            throw new NullQueryResultException("USER.USER_ID: "+userId);

        return inquiryList;
    }

    /**
     * 获取所有问诊信息
     * @return  问诊信息
     * @throws BaseException 异常
     */
    @Override
    public List<InquiryEntity> getAllInquiry() throws BaseException {
        List<InquiryEntity> inquiryList = null;
        try {
            inquiryList = inquiryMapper.queryAllInquiry();
        } catch(Exception e) {
            logger.error("[query inquiry failed]",e);
        }
        if(inquiryList == null || inquiryList.isEmpty())
            throw new NullQueryResultException("ALL INQUIRY ");
        return inquiryList;
    }

    /**
     * 修改问诊信息
     * @param inquiry 问诊信息
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeInquiry(InquiryEntity inquiry) throws BaseException {
        int result = 0;
        try {
            result = inquiryMapper.updateInquiry(inquiry);
        } catch(Exception e) {
            logger.error("[query inquiry failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE INQUIRY "+inquiry);
        return result;
    }

    /**
     * 增加问诊信息
     * @param inquiry 问诊信息
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addInquiry(InquiryEntity inquiry) throws BaseException {
        int result = 0;
        try {
            result = inquiryMapper.insertInquiry(inquiry);
        } catch (Exception e) {
            logger.error("[add inquiry failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT INQUIRY: "+inquiry);

        return result;
    }

    /**
     * 删除问诊信息
     * @param caseId 问诊id
     * @return  改动数量
     * @throws BaseException
     */
    @Override
    public int deleteInquiry(Long caseId) throws BaseException {
        int result = 0;
        try {
            if(prescriptionMapper.queryPrescriptionByInquiry(caseId).isEmpty()
                && talkRoomMapper.queryTalkRoomByInquiry(caseId).isEmpty())
                result = inquiryMapper.deleteInquiry(caseId);
        } catch (Exception e) {
            logger.error("[delete inquiry failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("DELETE INQUIRY.ID: "+caseId);
        return result;
    }
}
